package guda.sqladmin.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import guda.sqladmin.biz.ProjectBiz;
import guda.sqladmin.biz.query.FieldQuery;
import guda.sqladmin.dao.ProjectDOMapper;
import guda.sqladmin.dao.ProjectTableDOMapper;
import guda.sqladmin.dao.domain.ProjectDO;
import guda.sqladmin.dao.domain.ProjectTableDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import guda.sqladmin.biz.TableFieldBiz;
import guda.sqladmin.dao.domain.TableFieldDO;
import guda.sqladmin.web.form.TableFieldEditForm;
import guda.sqladmin.web.form.TableFieldForm;
import guda.tools.web.page.BaseQuery;
import guda.tools.web.page.BizResult;
import guda.tools.web.util.RequestUtil;


@Controller
public class TableFieldAction {


    @Autowired
    private TableFieldBiz tableFieldBiz;
    @Autowired
    private ProjectDOMapper projectDOMapper;
    @Autowired
    private ProjectTableDOMapper projectTableDOMapper;

    @RequestMapping(value = "tableField/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        long tableId = RequestUtil.getLong(request,"tableId");
        ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(tableId);
        modelMap.put("table",projectTableDO);
        ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
        modelMap.put("project",projectDO);
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        FieldQuery fieldQuery = new FieldQuery();
        fieldQuery.setPageNo(pageId);
        fieldQuery.setPageSize(pageSize);
        fieldQuery.setTableId(tableId);
        modelMap.put("query",fieldQuery);
        BizResult bizResult = tableFieldBiz.list(fieldQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "tableField/list.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "tableField/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, TableFieldEditForm tableFieldEditForm,
        BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = tableFieldBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            TableFieldDO tableFieldDO =  ((TableFieldDO)bizResult.data.get("tableFieldDO"));
            tableFieldEditForm.initForm(tableFieldDO);

            ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(tableFieldDO.getTableId());
            modelMap.put("table",projectTableDO);
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
            modelMap.put("project",projectDO);
            return "tableField/edit.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "tableField/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = tableFieldBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            TableFieldDO tableFieldDO =  ((TableFieldDO)bizResult.data.get("tableFieldDO"));
            ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(tableFieldDO.getTableId());
            modelMap.put("table",projectTableDO);
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
            modelMap.put("project",projectDO);
            return "tableField/detail.vm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "tableField/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, TableFieldForm tableFieldForm,
        BindingResult result, Map<String,Object> model) {
        long tableId = RequestUtil.getLong(request,"tableId");
        ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(tableId);
        modelMap.put("table",projectTableDO);
        ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
        modelMap.put("project",projectDO);
        tableFieldForm.setTableId(tableId);
        return "tableField/create.vm";
    }

    @RequestMapping(value = "tableField/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid  TableFieldForm tableFieldForm,
        BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            ProjectTableDO projectTableDO = projectTableDOMapper.selectByPrimaryKey(tableFieldForm.getTableId());
            modelMap.put("table",projectTableDO);
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(projectTableDO.getProjectId());
            modelMap.put("project",projectDO);
            return "tableField/create.vm";
        }
        TableFieldDO tableFieldDO = tableFieldForm.toDO();
        BizResult bizResult = tableFieldBiz.create(tableFieldDO);
        if (bizResult.success) {
            return "redirect:/tableField/list.htm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "tableField/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid TableFieldEditForm tableFieldEditForm,
        BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            return "tableField/edit.vm";
        }
        TableFieldDO tableFieldDO = tableFieldEditForm.toDO();
        BizResult bizResult = tableFieldBiz.update(tableFieldDO);
        if (bizResult.success) {
            return "redirect:/tableField/list.htm";
        } else {
            return "common/error.vm";
        }

    }

    @RequestMapping(value = "tableField/doDelete.htm")
    public String doDelete(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = tableFieldBiz.delete(id);
        if (bizResult.success) {
            return "tableField/list.htm";
        } else {
            return "common/error.vm";
        }

    }



}
